using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VFDECK
    {
        public DataRow Convert2FDECK(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryFDECK(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["FDMANDANT"] = " ".PadRight(2 ); 
         dwReturn["FDSA"] = " ".PadRight(1 ); 
         dwReturn["FDAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["FDAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["FDAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["FDTYP"] = " ".PadRight(1 ); 
         dwReturn["FDAFOLGE"] = " ".PadRight(3 ); 
         dwReturn["FDSETPOS"] = " ".PadRight(4 ); 
         dwReturn["FDART"] = " ".PadRight(20 ); 
         dwReturn["FDVAR"] = " ".PadRight(2 ); 
         dwReturn["FDFARBE"] = " ".PadRight(10 ); 
         dwReturn["FDGROE"] = " ".PadRight(7 ); 
         dwReturn["FDAUFM"] = " ".PadRight(2 ); 
         dwReturn["FDVPEINH"] = " ".PadRight(3 ); 
         dwReturn["FDZS"] = " ".PadRight(2 ); 
         dwReturn["FDDAT"] = " ".PadRight(0 ); 
         dwReturn["FDLO"] = " ".PadRight(2 ); 
         dwReturn["FDLB"] = " ".PadRight(2 ); 
         dwReturn["FDLP"] = " ".PadRight(8 ); 
         dwReturn["FDAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["FDMENGE"] = " ".PadRight(0 ); 
         dwReturn["FDZUGET"] = " ".PadRight(0 ); 
         dwReturn["FDFREIG"] = " ".PadRight(0 ); 
         dwReturn["FDVERBR"] = " ".PadRight(0 ); 
         dwReturn["FDRESERV"] = " ".PadRight(0 ); 
         dwReturn["FDKOMMISS"] = " ".PadRight(0 ); 
         dwReturn["FDSETANZ"] = " ".PadRight(0 ); 
         dwReturn["FDERLKZ"] = " ".PadRight(1 ); 
         dwReturn["FDDISPOKZ"] = " ".PadRight(1 ); 
         dwReturn["FDABRKZ"] = " ".PadRight(1 ); 
         dwReturn["FDFILLER"] = " ".PadRight(36 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapFDECK(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveFDECK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryFDECK(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapFDECK(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertFDECK(dwLocal, ITXDBVERSION);
            }
            UpdateFDECK(dwLocal);
        }
        public void InsertFDECK(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO FDECK(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryFDECK(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM FDECK WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM FDECK  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "FDECK";
            Return = Result;
            return Return;
        }
        public void DeleteFDECK(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM FDECK   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateFDECK(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE FDECK SET "
                    +"FDMANDANT= :FDMANDANT,"
                    +"FDSA= :FDSA,"
                    +"FDAUFKRS= :FDAUFKRS,"
                    +"FDAUFLFD= :FDAUFLFD,"
                    +"FDAUFPOS= :FDAUFPOS,"
                    +"FDTYP= :FDTYP,"
                    +"FDAFOLGE= :FDAFOLGE,"
                    +"FDSETPOS= :FDSETPOS,"
                    +"FDART= :FDART,"
                    +"FDVAR= :FDVAR,"
                    +"FDFARBE= :FDFARBE,"
                    +"FDGROE= :FDGROE,"
                    +"FDAUFM= :FDAUFM,"
                    +"FDVPEINH= :FDVPEINH,"
                    +"FDZS= :FDZS,"
                    +"FDDAT= :FDDAT,"
                    +"FDLO= :FDLO,"
                    +"FDLB= :FDLB,"
                    +"FDLP= :FDLP,"
                    +"FDAUFBKZ= :FDAUFBKZ,"
                    +"FDMENGE= :FDMENGE,"
                    +"FDZUGET= :FDZUGET,"
                    +"FDFREIG= :FDFREIG,"
                    +"FDVERBR= :FDVERBR,"
                    +"FDRESERV= :FDRESERV,"
                    +"FDKOMMISS= :FDKOMMISS,"
                    +"FDSETANZ= :FDSETANZ,"
                    +"FDERLKZ= :FDERLKZ,"
                    +"FDDISPOKZ= :FDDISPOKZ,"
                    +"FDABRKZ= :FDABRKZ,"
                    +"FDFILLER= :FDFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("FDMANDANT", OracleType.NVarChar).Value = dwData["FDMANDANT"].ToString(); 
          or1.Parameters.Add("FDSA", OracleType.NVarChar).Value = dwData["FDSA"].ToString(); 
          or1.Parameters.Add("FDAUFKRS", OracleType.NVarChar).Value = dwData["FDAUFKRS"].ToString(); 
          or1.Parameters.Add("FDAUFLFD", OracleType.NVarChar).Value = dwData["FDAUFLFD"].ToString(); 
          or1.Parameters.Add("FDAUFPOS", OracleType.NVarChar).Value = dwData["FDAUFPOS"].ToString(); 
          or1.Parameters.Add("FDTYP", OracleType.NVarChar).Value = dwData["FDTYP"].ToString(); 
          or1.Parameters.Add("FDAFOLGE", OracleType.NVarChar).Value = dwData["FDAFOLGE"].ToString(); 
          or1.Parameters.Add("FDSETPOS", OracleType.NVarChar).Value = dwData["FDSETPOS"].ToString(); 
          or1.Parameters.Add("FDART", OracleType.NVarChar).Value = dwData["FDART"].ToString(); 
          or1.Parameters.Add("FDVAR", OracleType.NVarChar).Value = dwData["FDVAR"].ToString(); 
          or1.Parameters.Add("FDFARBE", OracleType.NVarChar).Value = dwData["FDFARBE"].ToString(); 
          or1.Parameters.Add("FDGROE", OracleType.NVarChar).Value = dwData["FDGROE"].ToString(); 
          or1.Parameters.Add("FDAUFM", OracleType.NVarChar).Value = dwData["FDAUFM"].ToString(); 
          or1.Parameters.Add("FDVPEINH", OracleType.NVarChar).Value = dwData["FDVPEINH"].ToString(); 
          or1.Parameters.Add("FDZS", OracleType.NVarChar).Value = dwData["FDZS"].ToString(); 
          or1.Parameters.Add("FDDAT", OracleType.NVarChar).Value = dwData["FDDAT"].ToString(); 
          or1.Parameters.Add("FDLO", OracleType.NVarChar).Value = dwData["FDLO"].ToString(); 
          or1.Parameters.Add("FDLB", OracleType.NVarChar).Value = dwData["FDLB"].ToString(); 
          or1.Parameters.Add("FDLP", OracleType.NVarChar).Value = dwData["FDLP"].ToString(); 
          or1.Parameters.Add("FDAUFBKZ", OracleType.NVarChar).Value = dwData["FDAUFBKZ"].ToString(); 
          or1.Parameters.Add("FDMENGE", OracleType.NVarChar).Value = dwData["FDMENGE"].ToString(); 
          or1.Parameters.Add("FDZUGET", OracleType.NVarChar).Value = dwData["FDZUGET"].ToString(); 
          or1.Parameters.Add("FDFREIG", OracleType.NVarChar).Value = dwData["FDFREIG"].ToString(); 
          or1.Parameters.Add("FDVERBR", OracleType.NVarChar).Value = dwData["FDVERBR"].ToString(); 
          or1.Parameters.Add("FDRESERV", OracleType.NVarChar).Value = dwData["FDRESERV"].ToString(); 
          or1.Parameters.Add("FDKOMMISS", OracleType.NVarChar).Value = dwData["FDKOMMISS"].ToString(); 
          or1.Parameters.Add("FDSETANZ", OracleType.NVarChar).Value = dwData["FDSETANZ"].ToString(); 
          or1.Parameters.Add("FDERLKZ", OracleType.NVarChar).Value = dwData["FDERLKZ"].ToString(); 
          or1.Parameters.Add("FDDISPOKZ", OracleType.NVarChar).Value = dwData["FDDISPOKZ"].ToString(); 
          or1.Parameters.Add("FDABRKZ", OracleType.NVarChar).Value = dwData["FDABRKZ"].ToString(); 
          or1.Parameters.Add("FDFILLER", OracleType.NVarChar).Value = dwData["FDFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
